package dobest.ray.com.jiongjiong.views.chart;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import dobest.ray.com.jiongjiong.R;

/**
 * 横向滑动图标
 * 
 * @author ray
 * 
 */
public class HoriScrollView extends FrameLayout {
	private Context context;
	// 纵向刻度值
	private ListView ls_v_scale;
	private ScaleAdapter sAdapter;

	private RelativeLayout add_can_scroll_view_layout;
	private TextView tv_view_title;
	private TextView tv_scale_tag;
	// 刻度 可设置
	private List<String> scales = new ArrayList<String>();

	public HoriScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context;
		init();
	}

	public HoriScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}

	public HoriScrollView(Context context) {
		super(context);
		this.context = context;
		init();
	}

	private void init() {
		LayoutInflater.from(context).inflate(R.layout.hori_scroll_base_view,
				this);
		ls_v_scale = (ListView) findViewById(R.id.ls_v_scale);
		tv_view_title = (TextView) findViewById(R.id.tv_view_title);
		tv_scale_tag = (TextView) findViewById(R.id.tv_scale_tag);

		add_can_scroll_view_layout = (RelativeLayout) findViewById(R.id.add_can_scroll_view_layout);

	}

	class ScaleAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return scales.size();
		}

		@Override
		public Object getItem(int position) {
			return scales.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View view, ViewGroup parent) {
			if (view == null) {
				view = LayoutInflater.from(context).inflate(
						R.layout.item_hori_scroll_view, null);
			}
			TextView tv_item_scale = (TextView) view
					.findViewById(R.id.tv_item_scale);
			tv_item_scale.setText(scales.get(position));
			return view;
		}

	}

	private float max = 0;
	private float min = 0;
	private int scaleCount = 5;

	public void setSpecial(float max, float min) {
		this.max = max;
		this.min = min;
	}

	private boolean isShowInt = false;

	public void setShowInt(boolean isShowInt) {
		this.isShowInt = isShowInt;
	}

	public void setData(List<Float> left, List<Float> right,
			List<Float> danger, List<String> dateArray) {

		if (left == null || left.size() == 0)
			return;

		if (max == 0 && min == 0) {
			if (Collections.max(left) > Collections.max(right)) {
				max = Collections.max(left);
			} else {
				max = Collections.max(right);
			}
			if (Collections.min(left) > Collections.min(right)) {
				min = Collections.min(right);
			} else {
				min = Collections.min(left);
			}
			if (max < Collections.max(danger)) {
				max = Collections.max(danger);
			}
			if (min > Collections.min(danger)) {
				min = Collections.min(danger);
			}
			if (right.size() == 1) {
				if (min > 0) {
					min *= 0.8;
				} else {
					min *= 1.2;
				}
			}
		}

		// float per = (float) Math.ceil((max - min) / 6.0f / 1.1);
		float per = (float) ((max - min) / scaleCount);

		for (int i = 0; i < 6; i++) {
			BigDecimal b = new BigDecimal(((scaleCount - i) * per + min));
			if (isFloat) {
				scales.add((b.setScale(offestFloat, BigDecimal.ROUND_HALF_UP)
						.floatValue()) + "");
			} else {
				scales.add((int) (b.setScale(0, BigDecimal.ROUND_HALF_UP)
						.floatValue()) + "");
			}

		}
		sAdapter = new ScaleAdapter();
		ls_v_scale.setAdapter(sAdapter);

		add_can_scroll_view_layout.addView(new EyeChartView(context, left,
				right, danger, dateArray, max, min, isShowInt));
	}

	public void setTitle(String title) {
		tv_view_title.setText(title);
	}

	public void setScaleTagVisiable(boolean isVisiable) {
		if (isVisiable) {
			tv_scale_tag.setVisibility(View.VISIBLE);
		} else {
			tv_scale_tag.setVisibility(View.INVISIBLE);

		}
	}
	public void setScaleTag(String tag){
		tv_scale_tag.setText(tag);
	}

	private boolean isFloat = false;
	private int offestFloat = 0;

	public void setFloat(int offestFloat) {
		this.offestFloat = offestFloat;
		isFloat = true;
	}
}
